using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using Lib;
using System.Threading;

namespace Lib.SMS
{
    public class ExecAT
    {
#region Member
        SerialPort _port;

        public SerialPort Port
        {
            get { return _port; }
            set { _port = value; }
        }
        int _reponseTimeOut = 1000;

        public int ReponseTimeOut
        {
            get { return _reponseTimeOut; }
            set { _reponseTimeOut = value; }
        }
        AutoResetEvent autoEvent;

        public AutoResetEvent AutoEvent
        {
            get { return autoEvent; }
            set { autoEvent = value; }
        }
#endregion
        
        public ExecAT()
        {

        }
        public ExecAT(string pPort,int pBaudRate, int pDataBit,int pReadTimeOut
            ,int pWriteTimeOut)
        {
            _port=new SerialPort();
            _port.PortName = pPort;
            _port.BaudRate = pBaudRate;
            _port.DataBits = pDataBit;
            _port.ReadTimeout = pReadTimeOut;
            _port.WriteTimeout = pWriteTimeOut;
        }
        public SerialPort OpenPort()
        {
            
            try
            {
                if (Port.IsOpen == false)
                {
                    Port.Open();
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.ToString(),"Open port");
            }
            return Port;
        }
        public SerialPort ClosePort()
        {

            try
            {
                if (Port.IsOpen == true)
                {
                    Port.Close();
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.ToString(),"Close port");
            }
            return Port;
        }

        string ReadResponse()
        {
            string buffer = string.Empty;
            try
            {
                do
                {
                    if (autoEvent.WaitOne(_reponseTimeOut, false))
                    {
                        string t = _port.ReadExisting();
                        buffer += t;
                    }
                    else
                    {
                        if (buffer.Length > 0)
                            throw new ApplicationException("Response received is incomplete.");
                        else
                            throw new ApplicationException("No data received from phone.");
                    }
                }
                while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return buffer;
        }
        public string ExecCommand(string cmd)
        {
            _port.WriteLine(cmd);
            Thread.Sleep(5);
            string output = ReadResponse();
            return output;
        }
    }
}
